package websocket.app.messaging;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ListenersRegistry {
    private final static Log log = LogFactory.getLog(ListenersRegistry.class);
    
    private final static ListenersRegistry instance = new ListenersRegistry();
    
    private CopyOnWriteArrayList<MessageListener> listeners = new CopyOnWriteArrayList<>();
    
    public static ListenersRegistry getInstance() {
	return instance;
    }
    
    public void addListener(MessageListener listener) {
	listeners.add(listener);
	log.debug("Added new listener, total count " + listeners.size());
    }
    
    public List<MessageListener> getAllListeners() {
	return new ArrayList<>(listeners);
    }
    
    public void removeListener(String id) {
	for (MessageListener listener : listeners) {
	    if (listener.getId().equals(id)) {
		listeners.remove(listener);
		break;
	    }
	}
	log.debug("Removed listener, total count " + listeners.size());
    }
}
